package com.cameron.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cameron.educms.entity.CrmBanner;
import com.cameron.educms.service.CrmBannerService;
import com.cameron.utils.Result;
import com.hooper.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author cameron
 * @since 2022-06-22
 */
@RestController
@RequestMapping("/edu-cms/banner")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    //1.分页查询
    @GetMapping("page/{page}/{limit}")
    public Result page(@PathVariable int page, @PathVariable int limit) {
        Page<CrmBanner> bannerPage = new Page<CrmBanner>(page, limit);
        bannerService.page(bannerPage, null);
        return Result.ok().data("items", bannerPage.getRecords()).data("total", bannerPage.getTotal());
    }

    //2.添加
    @PostMapping
    public Result add(String bannerUrl) {
        CrmBanner banner = new CrmBanner();
        banner.setGmtCreate(new Date());
        banner.setGmtModified(new Date());
        banner.setIsDeleted(false);
        banner.setSort(RandomUtil.randomInt(20));
        banner.setTitle("banner-" + RandomUtil.randomInt(20));
        banner.setLinkUrl("https://www.baidu.com");
        banner.setImageUrl(bannerUrl);
        bannerService.save(banner);
        return Result.ok();
    }

    //3.获取
    @GetMapping("{id}")
    public Result get(@PathVariable String id) {
        return Result.ok().data("item", bannerService.getById(id));
    }

    //4.修改
    @PutMapping("update")
    public Result update(@RequestBody CrmBanner banner) {
        return bannerService.updateById(banner) ? Result.ok() : Result.error();
    }
}


